using System;
using AvantGameMachine.DataManagement;
using AvantGameMachine.Graphics;

namespace AvantGameMachine
{
	public class ConfigurationManager
	{
		internal ConfigurationManager()
		{
		}

        internal void Init()
        {
            AGMPlatform.SData.AddXML("configuration.xml", new Address("/"), 0);
        }

		internal StructuredData GetConfigVar(string address,int _index) {
			StructuredData data = AGMPlatform.SData.Get(new Address("System/config/"+address),_index);
			if (data == null) {
				data = new StructuredData("");
				data.Data = "";
			}
			return data;
		}

        internal int GetConfigInt(Address _address, int _default)
        {
            StructuredData dt = GetConfigVar(_address.ToString(), 0);
            if (dt != null)
                return dt.GetTextAsNumber(_default);

            return _default;
        }

        internal Size2D GetConfigSize2D(Address _address, Size2D _default)
        {
            StructuredData dt = GetConfigVar(_address.ToString(), 0);
            if (dt != null)
            {

                StructuredData childw = dt.GetNodeFromAddress(new Address(dt.Name + "/Width"), 0);
                StructuredData childh = dt.GetNodeFromAddress(new Address(dt.Name + "/Height"), 0);

                int w = _default.Width;
                if (childw != null)
                    w = childw.GetTextAsNumber(_default.Width);

                int h = _default.Height;
                if (childh != null)
                    h = childh.GetTextAsNumber(_default.Height);

                return new Size2D(w, h);
            }
            return _default;
        }

	}
}

